/**
 * Created by Administrator on 2017/9/11.
 */

let mailDao = require('../mysql/mailDao.js');
let util = require('./util.js');


let method = module.exports;

method.send = function (type, id, name, avatar, text, attachment, cb) {
    if (!avatar) {
        avatar = '';
    }
    let mailModel = new mailDao();
    mailModel.set('type', type);
    mailModel.set('userId', id);
    mailModel.set('source', name);
    mailModel.set('avatar', avatar);
    mailModel.set('time', util.timeStamp());
    mailModel.set('text', text);
    let i = 0;
    for (let k in attachment) {
        i++;
        mailModel.set('attachment' + i, k);
        mailModel.set('amount' + i, attachment[k]);
        if (i >= 3) {
            break;
        }
    }
    mailModel.save(function (err, res) {
        if (err != null) {
            return false;
        }
        if (cb) {
            cb(res.insertId);
        }
    });
};